package cn.linran.org.controller;

import cn.linran.basic.util.JsonResult;
import cn.linran.basic.util.PageList;
import cn.linran.org.domain.Employee;
import cn.linran.org.query.EmployeeQuery;
import cn.linran.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController // controller层，返回json格式数据
@RequestMapping("/employee")
@Api(value = "员工的API",description="员工相关的CRUD功能")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;


    @GetMapping
    public List<Employee> findAll(){
        return employeeService.loadAll();
    }


    @GetMapping("list/{id}")
    @ApiOperation(value = "通过店铺id查员工" )
    public List<Employee> loadEmpByShopId(@PathVariable("id") Long id){
        return employeeService.loadEmpByShopId(id);
    }
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    public Employee findOne( @ApiParam(name="编号",required = true) @PathVariable("id") Long id){
        return employeeService.loadById(id);
    }

    @DeleteMapping("/{id}")
    public JsonResult deleteById(@PathVariable("id") Long id){
        try {
            employeeService.remove(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("删除失败");
        }
    }

    @PutMapping
    public JsonResult updateOrAdd(@RequestBody Employee employee){
        try {
            if(employee.getId()==null){
                employeeService.save(employee);
            }else
                employeeService.update(employee);
            return  JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    /**
     * 分页
     * @param query
     * @return
     */
    @PostMapping
    public PageList<Employee> queryPage(@RequestBody EmployeeQuery query){
        try {
            return employeeService.pageQuery(query);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<Employee>();//第一次查
        }
    }
    /**
     * 批量删除
     * @param
     * @return
     */
    @PatchMapping()
    @ApiOperation(value = "批量删除" )
    public JsonResult patchDel(@RequestBody List<Long> ids){

        try {
            employeeService.batchDel(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网络延迟，稍后重试");
        }
    }


}























